package com.fc.mall.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.dto.CommonResult;
import com.fc.mall.dto.UmsWithdrawApproveParam;
import com.fc.mall.dto.UmsWithdrawQueryParam;
import com.fc.mall.model.UmsWithdraw;
import com.fc.mall.service.UmsWithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "UmsWithdrawController",description = "提现管理")
@RestController
@RequestMapping("/withdraw")
public class UmsWithdrawController {

    @Autowired
    private UmsWithdrawService umsWithdrawService;

    @ApiOperation("获取提现列表")
    @GetMapping("list")
    public CommonResult list(UmsWithdrawQueryParam queryParam,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum)
    {
        List<UmsWithdraw> umsWithdrawList = umsWithdrawService.getList(queryParam, pageNum, pageSize);
        return new CommonResult().pageSuccess(umsWithdrawList);
    }

    @ApiOperation("审核提现")
    @PutMapping("/{withdrawId}")
    public CommonResult approve(@PathVariable("withdrawId") Long withdrawId, @RequestBody UmsWithdrawApproveParam approveParam) {
        UmsWithdraw approved = umsWithdrawService.approve(withdrawId, approveParam);
        return new CommonResult().success(approved);
    }

    @InitBinder
    public void initBinder(final WebDataBinder binder){
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ApiOperation("获取提现不同状态数量")
    @GetMapping("status")
    public Result countStatus()
    {
        return Result.success(umsWithdrawService.countStatus());
    }
}
